package com.sky.service.impl;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Classname ShoppingCartServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/7/4 15:42
 * @Created by Ren
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private DishMapper dishMapper;

    /**
     * 添加购物车方法
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        log.info("传进来的参数购物车参数 {}", shoppingCartDTO);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());//添加用户id
        //查询表中有没有当前数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断表中有没有当前传进来的数据
        if (list != null && list.size() > 0) {
            //说明表中原来就有该数据
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1); //数量+1
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.update(shoppingCart);
        } else {
            //表中没有该数据
            //判断传进来的是套餐还是菜品
            if (shoppingCart.getDishId() != null) {
                //说明传进来的是菜品
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishId(dish.getId());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //说明传进来的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setSetmealId(setmeal.getId());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 返回购物车中的所有商品
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {

        //先把数据查出来,看number 是否是 0
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //将表中的数据赋给 shoppingCart
        shoppingCart = list.get(0);
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        //判断 -1 之后是否为0,为空直接删除
        if(shoppingCart.getNumber() == 0){
            //删除这条数据数据而不是修改数量和金额
            shoppingCartMapper.sub(shoppingCart);
        }else{
            //修改数据
            shoppingCartMapper.update(shoppingCart);
        }


    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {

        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

}
